package com.micai.springboot.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.micai.springboot.entity.MenuEntity;
import com.micai.springboot.mapper.MenuMapper;
import com.micai.springboot.service.MenuService;
import com.micai.springboot.vo.TreeMenuVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 描述：
 * <p>
 * Author: 赵新国
 * Date: 2017/10/24 18:36
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, MenuEntity> implements MenuService {

    private static final Logger logger = LoggerFactory.getLogger(MenuServiceImpl.class);

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public Page<MenuEntity> getMenuByPage(int pageNum, int pageSize, String name) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String, Object> columnMap = new HashMap<String, Object>(16);
        if (!StringUtils.isEmpty(name)) {
            columnMap.put("name", name);
        }
        Page<MenuEntity> menuEntities = menuMapper.findByPage(columnMap);
        logger.info("menuEntities: {}", JSONObject.toJSONString(menuEntities));
        if (menuEntities == null) {
            return null;
        }
        return menuEntities;
    }

    @Override
    public List<MenuEntity> getMenuByTree(String name) {
        Wrapper<MenuEntity> wrapper = new EntityWrapper<MenuEntity>();
        if (!StringUtils.isEmpty(name)) {
            wrapper.eq("name", name);
        }
        List<MenuEntity> menuEntities = menuMapper.selectList(wrapper);
        if (menuEntities == null) {
            return null;
        }

        /**
         * List to Tree
         */
        List<TreeMenuVo> menuVos = new ArrayList<TreeMenuVo>();
        for (MenuEntity menuEntity : menuEntities) {
            TreeMenuVo treeMenuVo = new TreeMenuVo();
            BeanUtils.copyProperties(menuEntity, treeMenuVo);
            menuVos.add(treeMenuVo);
        }

        List<TreeMenuVo> treeMenuVos = new ArrayList<TreeMenuVo>();
        for (TreeMenuVo treeMenuVo1 : menuVos) {
            boolean mark = false;
            for (TreeMenuVo treeMenuVo2 : menuVos) {
                if (treeMenuVo1.getpId() != null && treeMenuVo1.getpId().equals(treeMenuVo2.getId())) {
                    mark = true;
                    if (treeMenuVo2.getChildren() == null) {
                        treeMenuVo2.setChildren(new ArrayList<TreeMenuVo>());
                    }
                    treeMenuVo2.getChildren().add(treeMenuVo1);
                    break;
                }
            }
            if (!mark) {
                treeMenuVos.add(treeMenuVo1);
            }
        }
        logger.info("treeMenuVos: {}", JSONObject.toJSONString(treeMenuVos));

        return menuEntities;
    }

    public static void main(String [] args) {
        Map<String, Integer> map=new HashMap<String, Integer>();
        map.put("张三",22);
        map.put("李四",25);
        map.put("王五",33);
        map.put("赵六",28);
        map.put("田七",25);
        map.put("李思",25);
        map.put("李嘉欣",25);

        Set<Map.Entry<String, Integer>> set = map.entrySet();
        Iterator<Map.Entry<String, Integer>> iterator = set.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Integer> next = iterator.next();
            if (next.getValue().equals("25")) {
                iterator.remove();
            }
        }
        logger.info("删除指定key后的map：{}", JSONObject.toJSONString(map));
    }


}
